package com.woniuxy.courseweb.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniuxy.core.exception.ParameterValidException;
import com.woniuxy.core.model.Result;
import com.woniuxy.core.util.MyJwtUtil;
import com.woniuxy.core.util.PageInfo;
import com.woniuxy.coursedao.model.TeamCourse;
import com.woniuxy.coursemodel.form.OneTeamCourseForm;
import com.woniuxy.coursemodel.form.ScheduleForm;
import com.woniuxy.coursemodel.form.TeamCoursePageForm;
import com.woniuxy.coursemodel.param.OneTeamCourseParam;
import com.woniuxy.coursemodel.param.ScheduleParam;
import com.woniuxy.coursemodel.param.TeamCoursePageParam;
import com.woniuxy.courseservice.service.TeamCourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 团课课程表 前端控制器
 * </p>
 *
 * @author 冉杰
 * @since 2024-01-15
 */
@RestController
@RequestMapping("/teamCourse")
@Api(tags = "团课排课相关接口")
public class TeamCourseController {

    @Resource
    private TeamCourseService teamCourseService;

    /**
     *
     * @param form 团课排课参数列表（产品id，开始时间，结束时间）
     * @param token 场馆token
     * @return 无
     */
    @ApiOperation("团课排课方法")
    @PostMapping("/schedule.venue")
    public Result<?> schedule(@RequestBody @Validated ScheduleForm form,@RequestHeader String token){
        Integer venueId = MyJwtUtil.getVenueIdByKey(token, "venueId");
        ScheduleParam param = BeanUtil.toBean(form, ScheduleParam.class);
        param.setVenueId(venueId);
        teamCourseService.schedule(param);
        return Result.success("团课排课成功");
    }

    @ApiOperation("新增单个团课课程方法")
    @PostMapping("/one.venue")
    public Result<?> one(@RequestBody @Validated OneTeamCourseForm form, @RequestHeader String token){
        Integer venueId = MyJwtUtil.getVenueIdByKey(token, "venueId");
        OneTeamCourseParam param = BeanUtil.toBean(form, OneTeamCourseParam.class);
        param.setVenueId(venueId);
        teamCourseService.one(param);
        return Result.success("新增单个团课课程成功");
    }
    /**
     *
     * @param teamProductId 产品id
     * @param token 场馆token
     * @return 无
     */
    @ApiOperation("通过产品id删除团课方法")
    @GetMapping("/del.venue")
    public Result<?> del(Integer teamProductId,@RequestHeader String token){
        Integer venueId = MyJwtUtil.getVenueIdByKey(token, "venueId");
        teamCourseService.del(teamProductId,venueId);
        return Result.success("通过产品id删除团课成功");
    }

    /**
     *
     * @param teamCourseIds 团课课程ids
     * @param token 场馆token
     * @return 无
     */
    @ApiOperation("通过团课课程ids删除团课方法")
    @PostMapping("/delByIds.venue")
    public Result<?> delByIds(@RequestBody List<Integer> teamCourseIds, @RequestHeader String token){
        Integer venueId = MyJwtUtil.getVenueIdByKey(token, "venueId");
        teamCourseService.delByIds(teamCourseIds,venueId);
        return Result.success("通过团课课程ids删除团课成功");
    }

    @ApiOperation("分页查看团课课程")
    @PostMapping("/byPage.venue")
    public Result<PageInfo<TeamCourse>> byPage(@RequestBody @Validated TeamCoursePageForm form, BindingResult br , @RequestHeader String token){
        if (br.hasErrors()) throw new ParameterValidException(500,"参数输入有误");
        Integer venueId = MyJwtUtil.getVenueIdByKey(token, "venueId");
        TeamCoursePageParam param = BeanUtil.toBean(form, TeamCoursePageParam.class);
        param.setVenueId(venueId);
        PageInfo<TeamCourse> pageInfo = teamCourseService.byPage(param);
        return Result.success("分页查看团课课程成功",pageInfo);
    }
}

